﻿#include "ColdArcher.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../MirEnvir/Map.h"
#include "../MapObject.h"
#include "../../../Shared/Enums.h"
#include "../../../Shared/Data/Stat.h"
#include "../../Settings.h"
#include "../../../Shared/ServerPackets.h"
#include "../../MirEnvir/Envir.h"
#include "../DelayedAction.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    ColdArcher::ColdArcher(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool ColdArcher::InAttackRange()
    {
        if (getTarget()->getCurrentMap() != getCurrentMap())
        {
            return false;
        }

        return getTarget()->getCurrentLocation() != getCurrentLocation() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), AttackRange);
    }

    void ColdArcher::FindTarget()
    {
        Map *Current = getCurrentMap();

        for (int d = 0; d <= Info->ViewRange; d++)
        {
            for (int y = getCurrentLocation()->Y - d; y <= getCurrentLocation()->Y + d; y++)
            {
                if (y < 0)
                {
                    continue;
                }
                if (y >= Current->Height)
                {
                    break;
                }

                for (int x = getCurrentLocation()->X - d; x <= getCurrentLocation()->X + d; x += std::abs(y - getCurrentLocation()->Y) == d ? 1 : d * 2)
                {
                    if (x < 0)
                    {
                        continue;
                    }
                    if (x >= Current->Width)
                    {
                        break;
                    }
                    Cell *cell = Current->Cells[x][y];
                    if (cell->Objects.empty() || !cell->getValid())
                    {
                        continue;
                    }
                    for (int i = 0; i < cell->Objects.size(); i++)
                    {
                        MapObject *ob = cell->Objects[i];
                        switch (ob->getRace())
                        {
                            case ObjectType::Monster:
                                if (ob == this)
                                {
                                    continue;
                                }
                                if (!ob->IsFriendlyTarget(this))
                                {
                                    continue;
                                }
                                if (ob->getHidden())
                                {
                                    continue;
                                }
                                setTarget(ob);
                                return;
                            default:
                                continue;
                        }
                    }
                }
            }
        }
    }

    void ColdArcher::CompleteAttack(std::vector<std::any> &data)
    {
        MapObject *target = std::any_cast<MapObject*>(data[0]);
        int damage = std::any_cast<int>(data[1]);
        DefenceType defence = std::any_cast<DefenceType>(data[2]);



        if (getTarget() == nullptr)
        {
            return;
        }

        std::vector<MapObject*> targets = FindAllNearby(12, getTarget()->getCurrentLocation());

        for (int i = 0; i < targets.size(); i++)
        {
            target = targets[i];
            if (target == nullptr || !target->IsFriendlyTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
            {
                continue;
            }

            if (target->IsFriendlyTarget(this))
            {
                BuffType type = BuffType::MonsterMACBuff;

                auto stats = new Stats();
                stats.[Stat::HP] = 1000;

                target->AddBuff(type, this, Settings::Second * 10, stats);
                target->OperateTime = 0;

//C# TO C++ CONVERTER TODO TASK: A 'delete stats' statement was not added since stats was passed to a method or constructor. Handle memory management manually.
            }
        }
    }

    void ColdArcher::Attack()
    {
        if (getTarget() == nullptr)
        {
            return;
        }

        if (!getTarget()->IsFriendlyTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        int delay = Functions::MaxDistance(getCurrentLocation(), getTarget()->getCurrentLocation()) * 50 + 500;

        S::ObjectRangeAttack *tempVar = new S::ObjectRangeAttack();
        tempVar->ObjectID = ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = getCurrentLocation();
        tempVar->TargetID = getTarget()->ObjectID;
        tempVar->Type = 1;
        Broadcast(tempVar);
        //ActionList.Add(new DelayedAction(DelayedType.RangeDamage, Envir.Time + delay)); //Time for projectiles should always be calculated through their distance
        DelayedAction *action = new DelayedAction(DelayedType::RangeDamage, getEnvir()->getTime() + 500, {getTarget(), DefenceType::MACAgility});
        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        delete action;
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }
}
